<?php

namespace App\Http\Controllers;

use App\Novel;
use App\Category;
use Illuminate\Http\Request;

class NovelController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.novel.index',['list'=>Novel::paginate(5)]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.novel.add',['cateList'=>Category::get()]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $para=$request->all();
        is_null($request->novelimg)?Novel::create(array_merge($para,['novelimg'=>'nopic.jpg'])):Novel::create($para);
        
        
        return back();
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Novel  $novel
     * @return \Illuminate\Http\Response
     */
    public function show(Novel $novel)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Novel  $novel
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
        return view('admin.novel.edit',['cateList'=>Category::get(),
        'novel'=>Novel::find($request->id)]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Novel  $novel
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        Novel::find($request->id)->update($request->all());
        return redirect()->route('admin.novel');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Novel  $novel
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        Novel::destroy($request->id);
        return back();
    }
    public function digNovel(Request $request)
    {
        Novel::where('id',$request->id)->update(['tuitime'=>\Carbon\Carbon::now()]);
        return back();
    }
}
